package com.reactor.core;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.SimpleByteBufferAllocator;
import org.apache.mina.filter.LoggingFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

public class ServerRun {
	  private static final int PORT = 9123;

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		 ByteBuffer.setUseDirectBuffers(false);
	        ByteBuffer.setAllocator(new SimpleByteBufferAllocator());

	        IoAcceptor acceptor = new SocketAcceptor();

	        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
	        cfg.getFilterChain().addLast( "logger", new LoggingFilter() );
	        cfg.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ), LineDelimiter.NUL)));

	        acceptor.bind( new InetSocketAddress(PORT), new TimeServerHandler(), cfg);
	        System.out.println("MINA Time server started.");

	}

}
